//
// Created by 32827 on 2024/7/3.
//
#include "yz_adc.h"
YZF_RESULT yz_adc_once_for_yourself_init(YZF_ADC_POINTER adc,uint16_t channel,YZF_GPIO_POINTER port,YZF_GPIO_PIN pin){
    YGPIO(port)
    YPIN(pin)
    yzf_gpio_open_clock_();
    yzf_gpio_set_IN_ANALOG_();
    yzf_adc_clock_open(adc);

    yzf_adc_SMPR(adc,channel,0b111);
    yzf_adc_SQR_Counts_set(adc,1);
    yzf_adc_SQR_set(adc,channel,1);
//    ADC->CCR |= ADC_CCR_TSVREFE;
    yzf_adc(adc,YZF_TRUE);
    return YZF_OK;
}
uint16_t yz_adc_once_for_yourself_get(YZF_ADC_POINTER adc){
    yzf_adc_SWSTART(adc);
    while (yzf_adc_flag_STRT(adc,YZF_TRUE)!=YZF_TRUE){}
    while (yzf_adc_flag_EOC(adc,YZF_TRUE)!=YZF_TRUE){}
    return yzf_adc_DR_get(adc);
}
YZF_RESULT yz_adc_once_for_yourself_deInit(YZF_ADC_POINTER adc){
    yzf_adc(adc,YZF_FALSE);
    return YZF_OK;
}
//    ADC1->CR1 |= (2UL<<24); //8 bit ADC
//    ADC1->CR2 |= (1UL<<1);  //Enable continuous conversion
//    ADC1->CR2 |= (1UL<<10); //Enable EOC after every conversion
//    ADC1->SMPR2 |= (7UL<<3);//Select sampling time as 480 cycles for channel 1
//    GPIOA->MODER |= (3UL<<2); //Configure PA1 in analog mode
//    ADC1->SQR3 |= (1<<0);//Select channel 1 in ADC (Corresponds to PA1)

//    RCC->AHB1ENR |=(1UL<<22);//Enable DMA2 clock
//    ADC1->CR2 |= (1UL<<8);  //Enable DMA
//    ADC1->CR2 |= (1UL<<9);
//    DMA2->LIFCR |= 0x7DUL;//Clear all interrupt flags
//    DMA2_Stream0->CR &=~(7UL<<25);//Select channel 0 of DMA
//    DMA2_Stream0->CR &=~(3UL<<13);//Set memory of DMA to 8bit per data
//    DMA2_Stream0->CR &=~(3UL<<11);//Peripheral size to 8 bit per data
//    DMA2_Stream0->CR |=(1UL<<10);//Memory increment mode
//    DMA2_Stream0->CR &=~(3UL<<6);//Data transfer from peripheral to memory
//    DMA2_Stream0->CR |=(3UL<<3);//Transfer full and half complete interrupt Enable
//    DMA2_Stream0->CR |= (1UL << 8);//Enable circular mode
//    DMA2_Stream0->NDTR=16*1024;//Set DMA length to 8000
//    DMA2_Stream0->M0AR=(uint32_t)(uint16_t*)adc_data;//Select destination memory address as buffer declared
//    reg=(uint32_t)&ADC1->DR;
//    DMA2_Stream0->PAR=reg;//Select peripheral address as ADC1 data register
//    NVIC_EnableIRQ(DMA2_Stream0_IRQn);//Enable DMA Interrupt
//    dma_config();

//    DMA2_Stream0->CR |=(1UL<<0);//Enable DMA
//    ADC1->CR2 |= (1UL<<0); // Start the ADC
//    ADC1->CR2 |= (1UL<<30);// Start the conversion 软件启动
extern void user_pp_adc_update();
void DMA2_Stream0_IRQHandler(void)
{
//    user_pp_adc_update();
    DMA2->LIFCR |= 0x7DUL;//Clear all interrupt flags
}
YZF_RESULT yz_adc_data_for_dma(YZF_GPIO_POINTER gpio,YZF_GPIO_PIN gpio_pin,YZF_ADC_POINTER adc,uint16_t adc_channel,uint16_t adc_tsel,YZF_DMA_POINTER dma,YZF_DMA_CHANNEL_POINTER stream,uint16_t dma_channel,YZF_TIM_POINTER tim,uint16_t prescaler,uint16_t autoNumber,uint32_t data,uint16_t count){
    YGPIO(gpio)
    YPIN(gpio_pin)
    yzf_gpio_open_clock_();
    yzf_gpio_set_IN_ANALOG_();
    yzf_gpio_set_af_(0);

    yzf_adc_clock_open(adc);
    yzf_adc_resolution_set(adc,YZF_ADC_RESOLUTION_12BIT);
    yzf_adc_CONT(adc,YZF_TRUE);
    yzf_adc_EOCS(adc,YZF_TRUE);
    yzf_adc_SMPR(adc,adc_channel,0b11);
    yzf_adc_SQR_Counts_set(adc,1);
    yzf_adc_SQR_set(adc,adc_channel,1);
//    yzf_adc_EXTEN(adc,0b01);
//    yzf_adc_EXTSEL(adc,adc_tsel);

    yzf_dma_clock_enable(dma);
    yzf_adc_dma_enable(adc,YZF_TRUE);
    yzf_adc_dma_disable_selection(adc,YZF_TRUE);

    yzf_dma_clear_all_flag(dma,dma_channel);
    yzf_dma_channel_select(stream,dma_channel);
    yzf_dma_mem_size(stream,YZF_DMA_HALF_WORD);
    yzf_dma_per_size(stream,YZF_DMA_HALF_WORD);
    yzf_dma_mode_mem_inc(stream,YZF_TRUE);
    yzf_dma_mode_per_mem(stream);
    yzf_dma_interrupt_TC(stream,YZF_TRUE);
//    yzf_dma_interrupt_HT(stream,YZF_TRUE);
    yzf_dma_mode_circular(stream,YZF_TRUE);
    yzf_dma_transmit_count(stream,count);
    yzf_dma_mem0_address(stream,data);
    yzf_dma_per_address(stream,(uint32_t)&adc->DR);

    ///need change
    yzf_nvic_irq_enable(DMA2_Stream0_IRQn);
    yzf_nvic_irq_set_priority(DMA2_Stream0_IRQn,0b00110001);
//    yzf_adc_interrupt_EOC_enable(adc);
//    yzf_nvic_irq_enable(ADC_IRQn);
//    yzf_nvic_irq_set_priority(ADC_IRQn,0b00110000);

//    yzf_tim_set_prescaler(tim,prescaler);
//    yzf_tim_set_dir(tim,YZF_FALSE);
//    yzf_tim_set_auto_reload_number(tim,autoNumber);
//    yzf_tim_auto_reload_disable(tim);
//    yzf_tim_select_mms(tim,0b010);

    yzf_dma_enable(stream);
    yzf_adc(adc,YZF_TRUE);
    yzf_adc_SWSTART(adc);
//    yzf_tim_enable(tim);
    return YZF_OK;
}
YZF_RESULT yz_adc_data_for_dma6(YZF_ADC_POINTER adc,uint16_t adc_channel1,uint16_t adc_channel2,uint16_t adc_channel3,uint16_t adc_channel4,uint16_t adc_channel5,uint16_t adc_channel6,uint16_t adc_tsel,YZF_DMA_POINTER dma,YZF_DMA_CHANNEL_POINTER stream,uint16_t dma_channel,YZF_TIM_POINTER tim,uint16_t prescaler,uint16_t autoNumber,uint32_t data,uint16_t count){
    yzf_adc_clock_open(adc);
    yzf_adc_resolution_set(adc,YZF_ADC_RESOLUTION_12BIT);
    yzf_adc_SCAN(adc,YZF_TRUE);
    yzf_adc_CONT(adc,YZF_TRUE);
    yzf_adc_EOCS(adc,YZF_TRUE);
    yzf_adc_SMPR(adc,adc_channel1,0b11);
    yzf_adc_SQR_Counts_set(adc,6);
    yzf_adc_SQR_set(adc,adc_channel1,1);
    yzf_adc_SQR_set(adc,adc_channel2,2);
    yzf_adc_SQR_set(adc,adc_channel3,3);
    yzf_adc_SQR_set(adc,adc_channel4,4);
    yzf_adc_SQR_set(adc,adc_channel5,5);
    yzf_adc_SQR_set(adc,adc_channel6,6);

    yzf_dma_clock_enable(dma);
    yzf_adc_dma_enable(adc,YZF_TRUE);
    yzf_adc_dma_disable_selection(adc,YZF_TRUE);


    yzf_dma_clear_all_flag(dma,dma_channel);
    yzf_dma_channel_select(stream,dma_channel);
    yzf_dma_mem_size(stream,YZF_DMA_HALF_WORD);
    yzf_dma_per_size(stream,YZF_DMA_HALF_WORD);
    yzf_dma_mode_mem_inc(stream,YZF_TRUE);
    yzf_dma_mode_per_mem(stream);
    yzf_dma_interrupt_TC(stream,YZF_TRUE);
    yzf_dma_mode_circular(stream,YZF_TRUE);
    yzf_dma_transmit_count(stream,count);
    yzf_dma_mem0_address(stream,data);
    yzf_dma_per_address(stream,(uint32_t)&adc->DR);

    yzf_nvic_irq_enable(DMA2_Stream0_IRQn);
    yzf_nvic_irq_set_priority(DMA2_Stream0_IRQn,0b00110001);


    yzf_dma_enable(stream);
    yzf_adc(adc,YZF_TRUE);
    yzf_adc_SWSTART(adc);
    return YZF_OK;
}